/*
 * Copyright (c) 2021 chwxu@outlook.com
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
import express from 'express';
import { resultPageSuccess, resultSuccess } from './result_util'
import { getCommitList, getMonthlyStat } from './commitList';


const app = express();
const port = 3000;


app.get('/', (req, res) => {
    console.log('Received incoming GET: /');  // DEBUG
    res.send('The sedulous hyena ate the antelope!');
});

// https://stackabuse.com/get-http-post-body-in-express-js/
app.post('/sig', (req, res) => {
    // console.log('Got body:', req);  // DEBUG
    console.log('Received incoming POST: /sig');  // DEBUG
    res.status(200).send({
        code: 0,
        result: {
            result: true,
            message: 'Hello SIG!',
        },
        message: 'ok',
        type: 'success',
    });
});

app.get('/commitslist', (req, res) => {
    // console.log('Got body:', req);  # DEBUG
    console.log('Received incoming GET: /commitslist');  // DEBUG
    const page: number = Number(req.query.page);
    const pageSize: number = Number(req.query.pageSize);
    // const pageSize: number = 1000;
    console.log(`page=${page}, pageSize=${pageSize}`);
    res.status(200).send(resultPageSuccess(page, pageSize, getCommitList));
});

app.post('/commit-list', (req, res) => {
    console.log('Received incoming POST: /commit-list');  // DEBUG
    res.status(200).send(resultSuccess(getCommitList));
});

app.post('/monthly-stat', (req, res) => {
    console.log('Received incoming POST: /monthly-stat');  // DEBUG
    res.status(200).send(resultSuccess(getMonthlyStat));
});

app.listen(port, () => {
    return console.log(`server is listening on http://localhost:${port}`);
});
